package com.sensePlusVR.activity.follow;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.x;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sensePlusVR.adapter.FollowAllAdapter;
import com.sensePlusVR.adapter.FollowDetailListAdapter;
import com.sensePlusVR.adapter.FollowDetailListAdapter.OnDownloadedRemovedListener;
import com.sensePlusVR.adapter.FollowDetailListAdapter.OnNotDownloadToDownloadedListener;
import com.sensePlusVR.adapter.FollowDetailListAdapter.OnVideoDetailDismissListener;
import com.sensePlusVR.adapter.FollowFollowAdapter;
import com.sensePlusVR.adapter.MarqueeVerAdapter;
import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.bean.ActorItem;
import com.sensePlusVR.bean.CollectionItem;
import com.sensePlusVR.bean.CommendItem;
import com.sensePlusVR.bean.EventBusBean;
import com.sensePlusVR.bean.GameItem;
import com.sensePlusVR.db.ImageLibrary;
import com.sensePlusVR.db.ImageLibrary.Callback;
import com.sensePlusVR.main.R;
import com.sensePlusVR.manager.CollectionFollowManager;
import com.sensePlusVR.manager.DataCenter;
import com.sensePlusVR.manager.EventListener;
import com.sensePlusVR.manager.GameItemManager;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.CollectionTopSort;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.DialogUtils;
import com.sensePlusVR.utils.EventBusUtils;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MySharedPreferences;
import com.sensePlusVR.utils.MyToast;
import com.sensePlusVR.widget.FullyLinearLayoutManager;
import com.sensePlusVR.widget.MarqueeVerTextView;
import com.sensePlusVR.widget.sliddel.ItemRemoveRecyclerView;
import com.sensePlusVR.widget.sliddel.OnItemClickListener;
@SuppressLint({ "InflateParams", "UseSparseArrays", "NewApi" })
@SuppressWarnings({"unused"})
@ContentView(R.layout.activity_follow_detail)
public class FollowDetailActivity extends BaseActivity implements OnClickListener{
	private static final String TAG = FollowDetailActivity.class.getSimpleName();
	private static final String EVENT_KEY=FollowDetailActivity.class.getSimpleName();
	private List<GameItem> gameDownedList ;
	private List<GameItem> gameNotDownloadList;
	
	private FollowDetailListAdapter notDownloadAdapter;
	private FollowDetailListAdapter downloadAdapter;
	
	private ImageView follow_detail_user_iv;
	private TextView follow_detail_title_tv;
	private MarqueeVerTextView follow_detail_summary;
	private TextView collection_not_download;
	private ItemRemoveRecyclerView notDownloadRecycler;
	private ItemRemoveRecyclerView downloadRecycler;
	private RelativeLayout downloaded_pull_down_relative;
	private RelativeLayout nodownload_pull_down_relative;
	private ImageView downloaded_pull_down_iv;
	private ImageView notDownloaded_pull_down_iv;
	private DataCenter dataCenter;
	
	private ActorItem actorItem;
	private int fromPage = -1;//从FollowAll进入还是FollowFollow进入
	
	private Map<Integer, CommendItem> commendMap; 
	
	@ViewInject(R.id.detail_honey_iv)
	private ImageView detail_honey_iv;
	
	private int followed;
	
	private final String KEY = this.getClass().getSimpleName();
	
	//summary设置滚动需要参数
	private ArrayList<String> list = new ArrayList<String>();
	private boolean isBreak = true;
	private static final int MAX_LENGTH = 34;
	private int startCount = 0;
	private int endCount = MAX_LENGTH;
	
	private MarqueeVerAdapter marqueeVerAdapter;
	
	//private VRApplication vrApplication;
	//private Handler handler;
	public static final int MSG_ACTIVITY_FULLY_DISPLAY = 100;
	private static final int MSG_DELAY_TIME = 200;
	
	EventListener listener = new EventListener(){
		
		@Override
		public void Notify(int event, Object data, Object Params) {
			if (Params == null || data == null)
				return;
			MyLog.e(TAG, "event=" + event);
			if (event == CollectionFollowManager.EventFollowAdded ||
				event == CollectionFollowManager.EventFollowDeled ||
				(event-CollectionFollowManager.EventErrorBase) == CollectionFollowManager.EventFollowAdded ||
				(event-CollectionFollowManager.EventErrorBase) == CollectionFollowManager.EventFollowDeled)
				updateFollowList(event, (Integer)data);

		}
	};
	
	EventListener addGameListener = new EventListener(){

		@SuppressWarnings("unchecked")
		@Override
		public void Notify(int event, Object data, Object Params) {
			// TODO Auto-generated method stub
			if (Params == null || data == null)
				return;
			MyLog.e(TAG, "event = "+event);
			if (event == GameItemManager.EventGameItemActorName) {
				List<GameItem> giList = (List<GameItem>)data;
				if(giList == null)return;
				MyLog.e(TAG, "add to adapter to list size="+giList.size());
				if(giList.size() > 0){
					if(gameNotDownloadList == null)gameNotDownloadList = new ArrayList<GameItem>();
					if (notDownloadAdapter != null) {
						MyLog.d(TAG, "adapter != null");
						notDownloadAdapter.addList(giList);
					}
				}
			}
		}
	};
	
	
	void updateFollowList(final int event, final Integer actorId){
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				int followed = 0;
				if (event == CollectionFollowManager.EventFollowAdded){
					followed = 1;
				}else if (event == CollectionFollowManager.EventFollowDeled){
					followed = 0;
				}else if (event >= CollectionFollowManager.EventErrorBase) {
					int errEvent = event;
					errEvent -= CollectionFollowManager.EventErrorBase;
					if (errEvent == CollectionFollowManager.EventFollowAdded){
						followed = 0;
					}else if (errEvent == CollectionFollowManager.EventFollowDeled) {
						followed = 1;
					}
				}
				if (actorItem != null){
					actorItem.setFollowed(followed);
				}
				updateFollowUI(followed);
				MyLog.d(TAG, "curFollowed : " + followed);
			}
		});
	}
	
	protected void updateFollowUI(final int actorFollowed) {
		followed = actorFollowed;
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (followed == 0) {
					detail_honey_iv.setImageResource(R.drawable.honey_addfollowicon_selector);
				} else if (followed == 1) {
					detail_honey_iv.setImageResource(R.drawable.honey_minusfollowicon_selector);
				}
			}
		});
	}

	@Override
	public void setMyContentView() {
		x.view().inject(this);
		
		gameDownedList = new ArrayList<GameItem>() ;
		gameNotDownloadList = new ArrayList<GameItem>();
		
		commendMap = new HashMap<Integer, CommendItem>();
		
		downloadAdapter = new FollowDetailListAdapter(FollowDetailActivity.this, gameDownedList,Constant.FROM_DOWNLOADED);
		
		notDownloadAdapter = new FollowDetailListAdapter(FollowDetailActivity.this, gameNotDownloadList,Constant.FROM_NOT_DOWNLOADED);
		
		
	}
	
	@Override
	public void getIntentData() {
		super.getIntentData();
		dataCenter = DataCenter.getInstans();
//		dataCenter.resetGameDb();
		Intent intent = getIntent();
		if(intent != null){
			Bundle bundle = intent.getExtras();
			MyLog.d(TAG, "bundle="+bundle);
			
			if(bundle != null){
				
				actorItem = (ActorItem) bundle.getParcelable(Constant.INTENT_FOLLOW_DETAIL_ACTORITEM);
				if(actorItem == null)return;
				MyLog.d(TAG, "actorItem="+actorItem);
				fromPage = bundle.getInt(Constant.INTENT_FOLLOW_DETAIL_ACTOR_FROM);
				MyLog.d(TAG, "fromPage="+fromPage);
				
				//MySharedPreferences.save2(this, MySharedPreferences.NAME_NOT_CLEAR, String.valueOf(actorItem.getId()), true);
				MySharedPreferences.save(this, String.valueOf(actorItem.getId()), true);
			}
			
			
		}
	}
	
	
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onMessageEvent(EventBusBean event){
		String eventMessage = event.getMessage();
		MyLog.d(TAG, "onMessageEvent : " + eventMessage);
		if(eventMessage.equals(EventBusUtils.EVENT_NOTIFY_FOLLOWDETAIL_ACTIVITY)){
			
			refreshData();
			
		}
		
	}
	
	@Override
	public void initView() {
		super.initView();
		AppManager.getInstance().addActivity(this);
			initCurViews();
	}
	
	@Override
	public void beforeView() {
		super.beforeView();
		
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		MyLog.d(TAG, "onNewIntent...");
	}
	
	public OnDownloadedRemovedListener onDownloadedRemovedListener = new OnDownloadedRemovedListener() {
		
		@Override
		public void onDownloadedRemoved(GameItem gameItem) {
			if(gameItem == null)return;
			MyLog.d(TAG, "onDownloadedRemoved gameItem : " + gameItem.toString());
			downloadAdapter.remove(gameItem);
			notDownloadAdapter.add(gameItem, 0);
		}
	};
	
	public OnNotDownloadToDownloadedListener onNotDownloadToDownloadedListener = new OnNotDownloadToDownloadedListener() {
		@Override
		public void onNotDownloadToDownloaded(GameItem gameItem) {
			MyLog.d(TAG, "onNotDownloadToDownloaded  gameItem : " + gameItem.toString());
			if(actorItem != null){
				String actorName = actorItem.getName() == null?"":actorItem.getName();
				if(actorName.equals(gameItem.getActors())){
					notDownloadAdapter.remove(gameItem);
					downloadAdapter.add(gameItem, 0);
				}
			}
		}
	};
	
	@org.xutils.view.annotation.Event(value = {R.id.title_bar_left_ib,R.id.detail_honey_linear,R.id.follow_detail_summary})
	private void setViewOnClick(View v){
		switch (v.getId()) {
		case R.id.title_bar_left_ib:
			AppManager.getInstance().finishActivity(this);
			break;
		case R.id.detail_honey_linear:
			MyLog.d(TAG, "detail_honey_linear");
			if(followed == 0){
				uploadFollow(actorItem);
				detail_honey_iv.setImageResource(R.drawable.honey_minusfollowicon_selector);
				MyToast.showMessage(FollowDetailActivity.this, R.string.follow_added);
				followed = 1;
			}else if(followed == 1){
				deleteDialog();
				followed = 0;
			}
			if (actorItem != null)
				actorItem.setFollowed(followed);
			break;
		case R.id.follow_detail_summary:
			
			follow_detail_summary.startScroll();
			
			marqueeVerAdapter.notifyDataSetChanged();
			
			break;
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.downloaded_pull_down_relative:
			MyLog.d(TAG, "downloaded_pull_down_relative");
			if(downloadRecycler.getVisibility() == View.VISIBLE){
				downloadRecycler.setVisibility(View.GONE);
				downloaded_pull_down_iv.setImageResource(R.drawable.list_unfold_selector);
			}else{
				downloadRecycler.setVisibility(View.VISIBLE);
				downloaded_pull_down_iv.setImageResource(R.drawable.list_fold_selector);
			}
			break;
		case R.id.collection_pull_down_relative:
			if(notDownloadRecycler.getVisibility() == View.VISIBLE){
				notDownloadRecycler.setVisibility(View.GONE);
				notDownloaded_pull_down_iv.setImageResource(R.drawable.list_unfold_selector);
			}else{
				notDownloadRecycler.setVisibility(View.VISIBLE);
				notDownloaded_pull_down_iv.setImageResource(R.drawable.list_fold_selector);
			}
			break;
		}
	}
	
	private void deleteDialog() {
		String message = getString(R.string.follow_delete);
		DialogUtils.showConfirmDialogConfirmCancel(FollowDetailActivity.this,
				null, message, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						cancelFollow(actorItem);
						detail_honey_iv.setImageResource(R.drawable.honey_addfollowicon_selector);
						dialog.dismiss();
					}
				}, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						followed = 1;
						dialog.dismiss();
					}
				});
	}
	
	/**
	 * 上传关注
	 * 
	 * @param
	 */
	private void uploadFollow(ActorItem actorItem) {
		if(actorItem != null)
		dataCenter.FollowActorAdd(actorItem.getId());
	}

	/**
	 * 取消关注
	 */
	private void cancelFollow(ActorItem actorItem) {
		if(actorItem != null)
		dataCenter.FollowActorDel(actorItem.getId());
	}
	
	
	private void refreshData(){
		//刷新关注人物的信息
		String actorName = "";
		
		String summary = "";
		
		String gameUrl = "";
		
		if(actorItem != null){
			followed = actorItem.getFollowed();//0 表示未关注  1表示已经关注
			
			actorName = actorItem.getName();
			
			summary = actorItem.getSummary();
			
			gameUrl = actorItem.getImgurl();
		}
			MyLog.d(TAG, "followed : " + followed);
		if(followed == 0){
			detail_honey_iv.setImageResource(R.drawable.honey_addfollowicon_selector);
		}else if(followed == 1){
			detail_honey_iv.setImageResource(R.drawable.honey_minusfollowicon_selector);
		}
		follow_detail_title_tv.setText(actorName);
		
		MyLog.d(TAG, "summary : " + summary);
		
		while (isBreak) {

			MyLog.d(TAG, "endCount : " + endCount);

			if (endCount >= summary.length()) {
				endCount = summary.length();
				isBreak = false;
			}

			String subText = summary.substring(startCount, endCount);

			list.add(subText);

			MyLog.d(TAG, "subText : " + subText);

			startCount = startCount + MAX_LENGTH;

			endCount = startCount + MAX_LENGTH;
		}
		
		
		marqueeVerAdapter.setData(list, 2);
		
		 follow_detail_summary.setAdapter(marqueeVerAdapter);

		ImageLibrary.getInstance().getImageBitmap(FollowDetailActivity.this, gameUrl, new Callback() {
			
			@Override
			public void onImageDownloaded(Bitmap bitmap) {
				MyLog.d(TAG, "bitmap : " + bitmap);
				if (bitmap != null)
					follow_detail_user_iv.setImageBitmap(bitmap);
			}
		});
		
		
		List<GameItem> allGameList = dataCenter.getGameListByActorName(actorName, true);
		if (allGameList == null) {
			allGameList = new ArrayList<GameItem>();
		}
		
		//
		commendMap.clear();
		
		gameDownedList.clear();
		
		gameNotDownloadList.clear();
		
		List<CollectionItem> clcItems = new ArrayList<CollectionItem>();
		
		
		//遍历总表
		for(GameItem gameItem:allGameList){
			
			CollectionItem clcItem = CollectionItem.query(gameItem.getId());
			
			if(clcItem!=null){
				
				if(clcItem.getDownloadState()==CollectionItem.DownloadFinished){
					//if(gameItem != null)
					//gameDownedList.add(gameItem);
					clcItems.add(clcItem);
					
				}else{
					if(gameItem != null)
					gameNotDownloadList.add(gameItem);
				}
				
				//有无踩赞
				CommendItem citem = dataCenter.getGameCommend(gameItem.getId());
				if (citem != null) {
					
					commendMap.put(gameItem.getId(), citem);
				}
				
				
			}else{
					
				gameNotDownloadList.add(gameItem);
				
			}
			
		}
		
		//按照指定字段排序
		Collections.sort(clcItems, new CollectionTopSort());
		
		for(CollectionItem clcItem : clcItems){
			GameItem gItem = GameItem.getGameById(clcItem.getGameId());
			if(gItem != null)
			gameDownedList.add(gItem);
		}
		
		downloadAdapter.setCommendMap(commendMap);
		
		notDownloadAdapter.setCommendMap(commendMap);
		
		downloadAdapter.refreshData(gameDownedList);
		
		notDownloadAdapter.refreshData(gameNotDownloadList);
		
	}
	
	
	private void initCurViews(/*int position*/) {
		follow_detail_user_iv = (ImageView)findViewById(R.id.follow_detail_user_iv);
		follow_detail_title_tv = (TextView)findViewById(R.id.follow_detail_title_tv);
		follow_detail_summary = (MarqueeVerTextView)findViewById(R.id.follow_detail_summary);
		collection_not_download = (TextView)findViewById(R.id.collection_not_download);
		downloadRecycler = (ItemRemoveRecyclerView)findViewById(R.id.downloaded_gridview);
		notDownloadRecycler = (ItemRemoveRecyclerView)findViewById(R.id.collection_gridview);
		downloaded_pull_down_relative = (RelativeLayout)findViewById(R.id.downloaded_pull_down_relative);
		nodownload_pull_down_relative = (RelativeLayout)findViewById(R.id.collection_pull_down_relative);
		downloaded_pull_down_iv = (ImageView)findViewById(R.id.downloaded_pull_down_iv);
		notDownloaded_pull_down_iv = (ImageView)findViewById(R.id.collection_pull_down_iv);
		downloaded_pull_down_relative.setOnClickListener(this);
		nodownload_pull_down_relative.setOnClickListener(this);
		
		
		marqueeVerAdapter = new MarqueeVerAdapter(follow_detail_summary);
		
		
		collection_not_download.setText(R.string.not_download);
		
		
		FullyLinearLayoutManager downloadLinearLayoutManager = new FullyLinearLayoutManager(this);
		downloadLinearLayoutManager.setSmoothScrollbarEnabled(true);
		downloadLinearLayoutManager.setAutoMeasureEnabled(true);
		downloadRecycler.setLayoutManager(downloadLinearLayoutManager);
		downloadRecycler.setNestedScrollingEnabled(false);
		downloadRecycler.setAdapter(downloadAdapter);
		downloadRecycler.setOnItemClickListener(onDownloadItemClickListener);
		
		FullyLinearLayoutManager notDownloadLinearLayoutManager = new FullyLinearLayoutManager(this);
		notDownloadLinearLayoutManager.setSmoothScrollbarEnabled(true);
		notDownloadLinearLayoutManager.setAutoMeasureEnabled(true);
		notDownloadRecycler.setLayoutManager(notDownloadLinearLayoutManager);
		notDownloadRecycler.setNestedScrollingEnabled(false);
		notDownloadRecycler.setAdapter(notDownloadAdapter);
		notDownloadRecycler.setOnItemClickListener(onNotDownloadItemClickListener);
	}
	
	
	OnVideoDetailDismissListener onVideoDetailDismissListener = new OnVideoDetailDismissListener() {
		
		@Override
		public void onVideoDetailDismiss() {
			followed = dataCenter.getActorById(actorItem.getId()).getFollowed();
			if(followed == 0){
				detail_honey_iv.setImageResource(R.drawable.honey_addfollowicon_selector);
			}else if(followed == 1){
				detail_honey_iv.setImageResource(R.drawable.honey_minusfollowicon_selector);
			}
		}
	};
	/**
	 * Downloaded监听
	 */
	OnItemClickListener onDownloadItemClickListener = new OnItemClickListener() {
		
		@Override
		public void onItemClick(View view, int position) {}
		
		@Override
		public void onTopClick(int position) {
			if(position < 0 || position >= gameDownedList.size())return;
			GameItem gameItemCur = gameDownedList.get(position);
			GameItem gameItemFirst = gameDownedList.get(0);
			MyLog.d(TAG,"onTopClick onDownloadItemClickListener gameItemCur : " + gameItemCur);
			downloadAdapter.move(gameItemCur);
			//更改数据库置顶状态
			CollectionItem clcItemCur = CollectionItem.query(gameItemCur.getId());
			CollectionItem clcItemFirst = CollectionItem.query(gameItemFirst.getId());
			if(clcItemCur == null || clcItemFirst == null)return;
			long curTopCount = clcItemCur.getTopSort() + 1 + clcItemFirst.getTopSort();
			clcItemCur.setTopSort(curTopCount);
			CollectionItem.saveOrUpdate(clcItemCur);
		}
		
		@Override
		public void onDeleteClick(int position) {
			if(position < 0 || position >= gameDownedList.size())return;
			GameItem gameItem = gameDownedList.get(position);
			MyLog.d(TAG,"onDeleteClick onDownloadItemClickListener gameItem : " + gameItem);
			
			deleteDownloadORConnection(gameItem);
			
		}

		@Override
		public void onDeleteState(int x,int y) {}
		
		public void recoveryDrawer() {};
	};
	
	/**
	 * NotDownload监听
	 */
	OnItemClickListener onNotDownloadItemClickListener = new OnItemClickListener() {
		
		@Override
		public void onItemClick(View view, int position) {}
		
		@Override
		public void onTopClick(int position) {}
		
		@Override
		public void onDeleteClick(int position) {}

		@Override
		public void onDeleteState(int x,int y) {
			
			
			MyLog.d(TAG, "onNotDownloadItemClickListener onDeleteState state : " + notDownloadRecycler.getmDeleteBtnState());
			
		if(downloadRecycler.getmDeleteBtnState() == ItemRemoveRecyclerView.BTN_OPENED){
				
			MyLog.d(TAG, "onNotDownloadItemClickListener onDeleteState...");
				
				notDownloadRecycler.recoveryClick(x, y);
				
				downloadRecycler.deleteBtnToClosed();
			}
		
		}
		
		public void recoveryDrawer() {};
	};
	
	public void deleteDownloadORConnection(final GameItem gameItem) {

		CollectionItem clcItem = CollectionItem.query(gameItem.getId());

		if (clcItem != null) {

			dialogDelete(gameItem, clcItem);
		}

	}
	

	/**
	 * 提示不同对话框
	 * 
	 * @param gameItem
	 * @param
	 */
	private void dialogDelete(final GameItem gameItem,
			final CollectionItem collectionItem) {
		String message = getString(R.string.download_delete);

		DialogUtils.showConfirmDialogConfirmCancel(this, null, message,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						dialog.dismiss();

						// 取消搜藏
						CollectionItem.cancelCollection(gameItem,true);

						if (onDownloadedRemovedListener != null) {

							onDownloadedRemovedListener
									.onDownloadedRemoved(gameItem);
						}
						
						//downloadRecycler.startRecovery();

					}
				}, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

						downloadRecycler.setmDeleteBtnState(ItemRemoveRecyclerView.BTN_OPENED);
						
						dialog.dismiss();
					}
				});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MyLog.d(TAG, "onResume...");
		Map<String, Handler> curHandlers = VRApplication.handlers;
		Iterator<Map.Entry<String, Handler>> it = curHandlers.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Handler> entry = it.next();
			if (entry.getKey().equals(FollowFollowAdapter.KEY)){
				Message msg = Message.obtain();
				msg.what = MSG_ACTIVITY_FULLY_DISPLAY;
				msg.obj = FollowFollowAdapter.KEY;
				entry.getValue().sendMessageDelayed(msg, MSG_DELAY_TIME);
			}else if(entry.getKey().equals(FollowAllAdapter.KEY)){
				Message msg = Message.obtain();
				msg.what = MSG_ACTIVITY_FULLY_DISPLAY;
				msg.obj = FollowAllAdapter.KEY;
				entry.getValue().sendMessageDelayed(msg, MSG_DELAY_TIME);
			}
			it.remove();
		}
		
		EventBus.getDefault().register(this);
		
		if(notDownloadAdapter!=null){
			
			notDownloadAdapter.onResume();
		}
		
		if(downloadAdapter!=null){
			
			downloadAdapter.onResume();
		}
		
		dataCenter.addFCEventListener(listener, this, EVENT_KEY);
		dataCenter.addGameEventListener(addGameListener, this, EVENT_KEY);
		
		//已下载删除下载监听
		downloadAdapter.setOnDownloadedRemovedListener(onDownloadedRemovedListener);
		
		notDownloadAdapter.setOnNotDownloadToDownloadedListener(onNotDownloadToDownloadedListener);
		
		downloadAdapter.setOnVideoDetailDismissListener(onVideoDetailDismissListener);

		//未下载下载完成监听
		
		notDownloadAdapter.setOnVideoDetailDismissListener(onVideoDetailDismissListener);
		
		refreshData();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		MyLog.d(TAG, "onPause...");
		EventBus.getDefault().unregister(this);
		
		if(notDownloadAdapter!=null){
			
			notDownloadAdapter.onPause();
		}
		
		if(downloadAdapter!=null){
			
			downloadAdapter.onPause();
			
		}
		
		dataCenter.addFCEventListener(null, this, EVENT_KEY);
		
		dataCenter.delGameEventListener(EVENT_KEY);
		
		downloadAdapter.setOnDownloadedRemovedListener(null);
		
		notDownloadAdapter.setOnNotDownloadToDownloadedListener(null);
		
		downloadAdapter.setOnVideoDetailDismissListener(null);

		//未下载下载完成监听
		
		notDownloadAdapter.setOnVideoDetailDismissListener(null);
		
	}

}
